/*
 * client_context.cpp
 *
 * 	Ex: 5
 *
 *  Created on: Jun 6, 2012
 *      Author: Ariel Ayash
 */

#include "client_context.h"

#include <cstring>

using namespace std;

ClientContext::~ClientContext()
{
	DBG_PRINT("client %s deleted\n", _name);
}

ClientContext::ClientContext()
{
	_name[0] = '\0';
}

const char* ClientContext::get_client_name() const
{
	return _name;
}

void ClientContext::set_client_name( const char *name )
{
	strncpy( _name, name, MAXIMAL_CLIENT_NAME_LENGTH );
}

void ClientContext::remove_from_followers_list( uint follower_sock )
{
	_followers_clients.erase( follower_sock );

	DBG_PRINT("socket %u doesn't follows client %s anymore - current number of followers: %lu \n",
			  follower_sock,
			  _name,
			  _followers_clients.size() );
}

void ClientContext::add_to_followers_list( uint follower_sock,
							 	 	 	ClientContext* follower_context )
{

	_followers_clients[follower_sock] = follower_context;

	DBG_PRINT("socket %u follows client %s - current number of followers: %lu\n",
			  follower_sock,
			  _name,
			  _followers_clients.size());

}

bool ClientContext::is_someone_follows()
{
	return ! _followers_clients.empty();
}

void ClientContext::get_followers_list( std::vector<ClientContext*> &list )
{
	map< uint, ClientContext* >::iterator it;

	for ( it = _followers_clients.begin(); it != _followers_clients.end(); ++it )
	{
		list.push_back( it->second );
	}

}


void ClientContext::add_to_followed_list( uint followed_sock, ClientContext* follow_context )
{
	_followed_clients[followed_sock] = follow_context;

	DBG_PRINT("client %s follows client %s - current number of clients that %s follows: %lu\n",
			_name,
			follow_context->get_client_name(),
			_name,
			_followed_clients.size());
}

void ClientContext::remove_from_followed_list( uint followed_sock )
{
	_followed_clients.erase( followed_sock );
	DBG_PRINT("client %s doesn't follows socket %u anymore - current number of clients that %s follows: %lu\n",
			  _name,
			  followed_sock,
			  _name,
			 _followed_clients.size());
}

void ClientContext::get_client_followers_socket( std::vector<uint> &list )
{
	map< uint, ClientContext* >::iterator it;

	for ( it = _followers_clients.begin(); it != _followers_clients.end(); ++it )
	{
		list.push_back( it->first );
	}

}

void ClientContext::get_followed_list( std::vector<ClientContext*> &list )
{
	map< uint, ClientContext* >::iterator it;

	for ( it = _followed_clients.begin(); it != _followed_clients.end(); ++it )
	{
		list.push_back( it->second );
	}
}

bool ClientContext::is_followed_empty()
{
	return _followed_clients.empty();
}

void ClientContext::clear_followed_list()
{
	_followed_clients.clear();
}

